#include "BulletFactory.h"
#include "CNumber.h"
#include "Config.h"
#include "SpriteAnimation.h"
#include "PoolObject.h"
#include "xml_tag.h"




BulletFactory::BulletFactory(void)
{
	_resourceData = NULL;
	_id = NULL;
}


BulletFactory::~BulletFactory(void)
{
	CC_SAFE_RELEASE(_resourceData);
	CC_SAFE_RELEASE(_id);
}

void BulletFactory::parseXml( xml_node node )
{
	char* id = (char*)node.child(XMLTAG_ID).text().as_string();
	float boundX = node.child(XMLTAG_BOUND).child(XMLTAG_X).text().as_float();
	float boundY = node.child(XMLTAG_BOUND).child(XMLTAG_Y).text().as_float();

	xml_node resources = node.child(XMLTAG_RESOURCES);

	for (xml_node j = resources.child(XMLTAG_RESOURCE); j; j = j.next_sibling(XMLTAG_RESOURCE))
	{
		CCDictionary* _resource = CCDictionary::create();
		char* type = (char*)j.child(XMLTAG_RESOURCE_TYPE).text().as_string();
		int posX = j.child(XMLTAG_RESOURCE_POSITION).child(XMLTAG_X).text().as_int();
		int posY = j.child(XMLTAG_RESOURCE_POSITION).child(XMLTAG_Y).text().as_int();
		float scaleX = j.child(XMLTAG_RESOURCE_SCALEX).text().as_float();
		float scaleY = j.child(XMLTAG_RESOURCE_SCALEY).text().as_float();

		xml_node nAnchorPoint = j.child(XMLTAG_RESOURCE_ANCHORPOINT);
		if (nAnchorPoint != xml_node(NULL))
		{
			float sAnchorX = nAnchorPoint.child(XMLTAG_X).text().as_float();
			float sAnchorY = nAnchorPoint.child(XMLTAG_Y).text().as_float();

			_resource->setObject(CCNumber<CCPoint>::create(ccp(sAnchorX, sAnchorY)), XMLTAG_RESOURCE_ANCHORPOINT);
		} else
			_resource->setObject(CCNumber<CCPoint>::create(CCPointZero), XMLTAG_RESOURCE_ANCHORPOINT);

		if (strcmp(type, RESOURCE_SPRITE) == 0)
		{
			char* spriteFrame = (char*)j.child(XMLTAG_RESOURCE_SPRITEFRAME).text().as_string();
			_resource->setObject(ccs(spriteFrame), XMLTAG_RESOURCE_SPRITEFRAME);
		} else if (strcmp(type, RESOURCE_SPRITEANIMATION) == 0)
		{
			char* texture = (char*)j.child(XMLTAG_RESOURCE_TEXTURE).text().as_string();
			char* plist = (char*)j.child(XMLTAG_RESOURCE_PLIST).text().as_string();
			char* prefix = (char*)j.child(XMLTAG_RESOURCE_PREFIX).text().as_string();
			int from = j.child(XMLTAG_RESOURCE_FROM).text().as_int();
			int to = j.child(XMLTAG_RESOURCE_TO).text().as_int();

			_resource->setObject(ccs(texture), XMLTAG_RESOURCE_TEXTURE);
			_resource->setObject(ccs(plist), XMLTAG_RESOURCE_PLIST);
			_resource->setObject(ccs(prefix), XMLTAG_RESOURCE_PREFIX);
			_resource->setObject(CCNumber<int>::create(from), XMLTAG_RESOURCE_FROM);
			_resource->setObject(CCNumber<int>::create(to), XMLTAG_RESOURCE_TO);
		}else if (strcmp(type, RESOURCE_PARTICLE) == 0)
		{
			char* fileName = (char*)j.child(XMLTAG_RESOURCE_FILENAME).text().as_string();
			_resource->setObject(ccs(fileName), XMLTAG_RESOURCE_FILENAME);
		}
		
		_resource->setObject(ccs(type), XMLTAG_RESOURCE_TYPE);
		_resource->setObject(CCNumber<CCPoint>::create(ccp(posX, posY)), XMLTAG_RESOURCE_POSITION);
		_resource->setObject(CCNumber<CCPoint>::create(ccp(scaleX, scaleY)), XMLTAG_RESOURCE_SCALE);

		_resourceData->addObject(_resource);
	}
	setId(ccs(id));
	setBound(CCSizeMake(boundX, boundY));
}

bool BulletFactory::init()
{
	setResourceData(CCArray::create());
	return true;
}

Bullet* BulletFactory::createBullet()
{
	//Bullet* bullet = Bullet::create();
	Bullet* bullet = PoolObject<Bullet>::sharedPoolObject()->getObjectWithId(_id->getCString());

	if (bullet->getId() != NULL)
	{
		return bullet;
	}

	bullet->setId(_id);
	bullet->setContentSize(_bound);

	for (int i = 0; i < _resourceData->count(); i++)
	{
		CCDictionary* resource = dynamic_cast<CCDictionary*>(_resourceData->objectAtIndex(i));
		
		CCPoint scale = dynamic_cast<CCNumber<CCPoint>*>(resource->objectForKey(XMLTAG_RESOURCE_SCALE))->getNumber();
		CCPoint sAnchorPoint = dynamic_cast<CCNumber<CCPoint>*>(resource->objectForKey(XMLTAG_RESOURCE_ANCHORPOINT))->getNumber();
		CCPoint position = dynamic_cast<CCNumber<CCPoint>*>(resource->objectForKey(XMLTAG_RESOURCE_POSITION))->getNumber();

		char* type = (char*)dynamic_cast<CCString*>(resource->objectForKey(XMLTAG_RESOURCE_TYPE))->getCString();
		if (strcmp(type, RESOURCE_SPRITE) == 0)
		{
			char* spriteFrame = (char*)dynamic_cast<CCString*>(resource->objectForKey(XMLTAG_RESOURCE_SPRITEFRAME))->getCString();

			CCSprite* sprite = CCSprite::createWithSpriteFrameName(spriteFrame);
			sprite->setAnchorPoint(sAnchorPoint);
			sprite->setPosition(position);
			sprite->setScaleX(scale.x);
			sprite->setScaleY(scale.y);

			bullet->addChild(sprite);
		}
		else if (strcmp(type, RESOURCE_SPRITEANIMATION) == 0)
		{
			char* texture = (char*)dynamic_cast<CCString*>(resource->objectForKey(XMLTAG_RESOURCE_TEXTURE))->getCString();
			char* plist = (char*)dynamic_cast<CCString*>(resource->objectForKey(XMLTAG_RESOURCE_PLIST))->getCString();
			char* prefix = (char*)dynamic_cast<CCString*>(resource->objectForKey(XMLTAG_RESOURCE_PREFIX))->getCString();

			int from = dynamic_cast<CCNumber<int>*>(resource->objectForKey(XMLTAG_RESOURCE_FROM))->getNumber();
			int to = dynamic_cast<CCNumber<int>*>(resource->objectForKey(XMLTAG_RESOURCE_TO))->getNumber();

			SpriteAnimation* spriteAni1 = SpriteAnimation::createWith(texture, plist, prefix, from, to);
			spriteAni1->playAnimation(true);
			spriteAni1->setPosition(position);
			spriteAni1->setScaleX(scale.x);
			spriteAni1->setScaleY(scale.y);
			spriteAni1->setAnchorPoint(sAnchorPoint);
			bullet->addChild(spriteAni1);
		} else if (strcmp(type, RESOURCE_PARTICLE) == 0)
		{
			char* fileName = (char*)dynamic_cast<CCString*>(resource->objectForKey(XMLTAG_RESOURCE_FILENAME))->getCString();
			CCParticleSystemQuad* particle = CCParticleSystemQuad::create(fileName);
			//CCParticleBatchNode
			particle->setPosition(position);
			particle->setScaleX(scale.x);
			particle->setScaleY(scale.y);
			particle->setTag(TAG_PARTICLE);
			bullet->addChild(particle);
		}
	}

	return bullet;
}
